<template>
  <div>
    <el-dialog
      title="提示"
      :visible="dialogVisible"
      width="35%"
      @close="close"
    >
      <el-form ref="ruleForm" label-width="120px" :model="formData" :rules="rules">
        <el-form-item label="部门名称" prop="name">
          <el-input v-model="formData.name" size="mini" placeholder="请输入2-10个字符" />
        </el-form-item>
        <el-form-item label="部门编码" prop="code">
          <el-input v-model="formData.code" size="mini" placeholder="请输入2-10个字符" />
        </el-form-item>
        <el-form-item label="部门负责人" prop="managerId">
          <el-select v-model="formData.managerId" size="mini" placeholder="请选择负责人">
            <el-option
              v-for="item in managerList"
              :key="item.id"
              :label="item.username"
              :value="item.id"
            />
          </el-select>

        </el-form-item>
        <el-form-item label="部门介绍" prop="introduce">
          <el-input v-model="formData.introduce" size="mini" type="textarea" :rows="3" placeholder="请输入1-100个字符" />
        </el-form-item>
        <el-row type="flex" justify="center">
          <el-button type="primary" size="mini" @click="addDepartment">确 定</el-button>
          <el-button size="mini" @click="close">取 消</el-button>
        </el-row>
      </el-form>

    </el-dialog>
  </div>
</template>

<script>
import { getDepartment, simple, addDepartment, getDepar, xiugai } from '@/api/department'
export default {
  props: {
    dialogVisible: {
      type: Boolean,
      default: false
    },
    nodeId: {
      type: Number,
      default: null
    }
  },
  data() {
    return {
      formData: {
        code: '',
        introduce: '',
        managerId: '',
        name: '',
        pid: null
      },
      rules: {
        code: [
          { required: true, message: '请输入部门编号', trigger: 'blur' },
          {
            trigger: 'blur',
            validator: async(rule, vaule, callback) => {
              let res = await getDepartment()
              if (this.formData.id) {
                res = res.filter(item => item.id !== this.formData.id)
              }

              if (res.some(item => item.code === vaule)) {
                callback(new Error('部门中已存在该编码'))
              } else {
                callback()
              }
            }
          }],
        introduce: [{ required: true, message: '请输入部门介绍', trigger: 'blur' }],
        managerId: [{ required: true, message: '请输入部门负责人名称', trigger: 'blur' }],
        name: [{ required: true, message: '请输入部门名称' },
          {
            trigger: 'blur',
            validator: async(rule, vaule, callback) => {
              let res = await getDepartment()
              if (this.formData.id) {
                res = res.filter(item => item.id !== this.formData.id)
              }
              if (res.some(item => item.name === vaule)) {
                callback(new Error('部门中已存在该编码'))
              } else {
                callback()
              }
            }
          }]
      },
      managerList: []
    }
  },
  async created() {
    const res = await simple()
    this.managerList = res
  },
  methods: {
    close() {
      this.$refs.ruleForm.resetFields()
      this.$emit('update:dialogVisible', false)
    },
    addDepartment() {
      this.$refs.ruleForm.validate(async(isOk) => {
        if (isOk) {
          if (this.formData.id) {
            await xiugai(this.formData)
          } else {
            await addDepartment({ ...this.formData, pid: this.nodeId })
          }
          this.$emit('updataGx')
          this.formData = {
            code: '',
            introduce: '',
            managerId: '',
            name: '',
            pid: null
          }
          this.close()
        }
      })
    },
    async getDepar() {
      const res = await getDepar(this.nodeId)
      console.log(res)
      this.formData = { ...res }
    }

  }
}
</script>

<style>

</style>
